/*
esp8266.h
offer exp8266 wifi module driver
designed by lunanting

----------------------
2020-08-27 22:04:44
create file
----------------------
*/
#ifndef __ESP8266_H
#define __ESP8266_H


#include "../../../bsp/base_type.h"
#include "../../../../include/aos/kernel.h"



/**************************callback function*************************/
extern void xp_esp8266_rec_api(u8 link,u32 len,char *data);     //处理结束消息接口


/*****************************data type define**********************************/


//esp8266 cmd number
typedef enum{
    esp8266_test=0,     //test AT
    esp8266_rst,        //重启
    esp8266_ate0,       //关闭回显
    esp8266_ate1,       //开启回显
    esp8266_restore,    //出厂复位
    esp8266_cwmode,     //配置wifi模式
    esp8266_cwjap,      //连接路由器
    esp8266_cwqap,      //断开AP
    esp8266_cifsr,      //查询ip地址
    esp8266_cipstart,    //连接服务端
    esp8266_cipsend,    //发送数据
    esp8266_cipmux,     //连接模式设置
    esp8266_cipsta,     //设置sta静态ip
    esp8266_cipserver,  //创建服务
    esp8266_cipclose,   //关闭连接
    esp8266_cipservermaxconn,   //设置服务允许建立的最大连接数，1~5
    esp8266_cipmode,        //传输模式，0=普通，1=透传
}ESP8266_cmd;


//buffer data 
typedef struct{
    u8 link;    //连接通道id
    u32 len;     //数据长度
    char data[100]; //数据
}ESP8266_buffer;

//ap class
typedef struct{
    char name[20];
    char password[20];
}ESP8266_ap;


//esp8266 class
typedef struct{
    u8 init;    //init status
    u8 port;    //uart port
    u8 response;    //response state,0=nothing,1=ok ,2= error
    u8 writeFlage;  //write send data flage,0=no,1=yes
    int speed;  //speed
    char gate[16];     //gate ip
    char ip[16];       //local ip
    u8 sendFlage;   //send ok status
    u8 connect_wifi;    //wifi connect status
    u8 ready;       //module ready status
    u8 mux;     //多连模式，1=多连
//    ESP8266_client *client[5];     //连接的客户端
}ESP8266_class;


//client class
typedef struct{
    u8 link_id;     //link id
    char local_ip[16];     //client local ip
    char target_ip[16];    //server ip
    int target_port;    //server port
    ESP8266_class *esp8266; //物理属性
}ESP8266_client;

//server class
typedef struct{
    u8 open;    //open status,1=open,0=close
    int port;   //server port;
    ESP8266_class *esp8266; 
}ESP8266_server;


/***********************************function extern***************************/
extern int xp_esp8266_init(int port,int speed); //esp8266 init 
extern int xp_esp8266_cmd(const ESP8266_cmd cmd,const char *param);    //esp8266 send data
extern int xp_esp8266_get_response(void);   //get response status
extern void xp_esp8266_set_response(int status);    //set response status
extern int xp_esp8266_check_ready(void);    //check module ready
extern int xp_esp8266_send(char *data,int len); //send data 
extern int xp_esp8266_connect_ap(char *wifi_name,char *wifi_password);  //connect to wifi ap
extern int xp_esp8266_config(u8 mux);   //module base config
extern int xp_esp8266_create_client(u8 link,char *ip,int port); //create a tcp client and connect to the server
extern int xp_esp8266_link_close(u8 link);  //close link
extern int xp_esp8266_create_server(u8 open,int port,ESP8266_server *obj);    //create a server and set server open state
extern int xp_esp8266_server_set(ESP8266_server *obj,bool sta);   //set server open status
extern int xp_esp8266_set_staip(char *ip);  //set sta mode static ip
extern int xp_esp8266_wifi_config(char *name,char *password);   //config wifi ap message
extern ESP8266_client *xp_esp8266_client_new(u8 link,char *server_ip,int server_port);  //new a tcp client
extern ESP8266_server *xp_esp8266_server_new(bool open,int port);   //new a server


extern int xp_esp8266_debug(char *type,char *fun,char *param);


#endif
//end of the file
